import Taro, { Component } from '@tarojs/taro'
import { View, Text } from '@tarojs/components'
import { AtAvatar, AtBadge,AtNoticebar } from 'taro-ui'
import './myInquisition.scss'
import touxiang from '../../img/touxiang3.png'
import app from '../../main.js'

export default class MyInquisitions extends Component {

    config = {
        navigationBarTitleText: '我的咨询'
    }
    constructor() {
        super(...arguments)
        this.state = {
            charArray: [
            ]
        }
    }
    handleClick(item) {
        var type = item.order_type != '随访订单' ? '问诊订单' : '随访';
        if (item.status == '待回复' || item.status == '咨询中' || item.status == '随访中' || item.status == '已支付' || item.status == '待随访') {
            app.navigateTo('../chatsub/pages/Chat/Chat?type='+type+'&relate_id='+item.relate_id+'&receiver_id='+item.doctor_id);
        } else {
            app.navigateTo('../chatsub/pages/chatdetail/chatdetail?type='+type+'&relate_id='+item.relate_id+'&receiver_id='+item.doctor_id);
        }
    }
    offect = 0;
    length = 25;
    total = 0;
    isFinlsh = false;

    //上拉加载
    onReachBottom() {
        if ((this.offect + this.length) < this.total) {
            app.toast('正在加载----')
            this.offect = this.offect + this.length;
            this.isFinlsh = false;
            //调用接口
            this.getData();
        } else {
            app.toast('没有更多数据了咯')
            this.isFinlsh = true;
        }
    }
    getData() {
        let me = this
        let data = { offset: this.offect, length: this.length}
        let dataArray = []
        app.post('web/myself/myAdviceList',data, function (res) {
            dataArray = me.state.charArray.concat(res.data.result.list)
            me.total = res.data.result.total;
            me.setState({
                charArray: dataArray
            })
            
        })
    }

    // 关闭所有socket链接
    componentDidShow() {
        this.setState({
            charArray: []
        });
        this.getData();
        console.log('1')
        app.closeSockets();
    }

    render() {
        return (
            <View className='bigBox'>
                {/* <AtNoticebar icon='volume-plus'>
                    提示：出诊病人之前页面空白
                    </AtNoticebar> */}
                { 
                    this.state.charArray.map((item) => {
                        return (
                            <View className='box' onClick={this.handleClick.bind(this, item)} style={item.status == '待接单' ? 'display:none;' : ''}>
                                <View>
                                    <AtBadge value={item.unread == 0 ? '' : item.unread} maxValue={99}>
                                        <AtAvatar
                                            className='imageSize'
                                            image={app.getPath(item.sender_head_image)}
                                            circle
                                        />
                                    </AtBadge>
                                </View>
                                <View className='textInfo'>
                                    <View className='left'>
                                        <View className='docName'>{item.sender_name}</View>
                                        <View className='info'>{item.content}</View>
                                    </View>
                                    <View className='right'>
                                        <View>
                                            {item.created_at}
                                        </View>
                                        <View className={item.status === "咨询结束" ? 'state2' : 'state1'}>
                                            {item.status}
                                        </View>
                                    </View>
                                </View>
                            </View>
                        )
                    })
                }
            </View>
            
        )
    }
}